package cn.lyl.canting.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.lyl.canting.R;
import cn.lyl.canting.adapter.OrderItemAdapter;
import cn.lyl.canting.bean.AllFood;
import cn.lyl.canting.bean.OrderListBean;
import cn.lyl.canting.bean.UserOrder;

//订单列表
public class OrderActivity extends AppCompatActivity implements View.OnClickListener {


    private Button order_select;
    private ImageView user_head;
    private TextView user_name;
    private ListView order_listview;
    private final Context context = this;
    public List<OrderListBean> orderListBeans;
    private Intent intent;
    private String phone;
    private Button order_setting;
    private static final String TAG = "OrderActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();
    }

    private void initView() {
        order_select = (Button) findViewById(R.id.order_select);
        user_head = (ImageView) findViewById(R.id.user_head);
        user_name = (TextView) findViewById(R.id.user_name);
        order_setting = (Button) findViewById(R.id.order_setting);
        order_listview = (ListView) findViewById(R.id.order_listview);
        intent = getIntent();
        phone = intent.getStringExtra("phone");
        orderListBeans = new ArrayList<OrderListBean>();
        updateUi();
        order_select.setOnClickListener(this);
        order_setting.setOnClickListener(this);
        //为listview中的item设置点击事件
        order_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, OrderContentActivity.class);
                //将商品信息传递并跳转至商品详情界面
                intent.putExtra("name", orderListBeans.get(position).getFoodName());
                intent.putExtra("content", orderListBeans.get(position).getFoodContent());
                intent.putExtra("price", orderListBeans.get(position).getAllPrice());
                intent.putExtra("img", orderListBeans.get(position).getImgId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击"点餐"按钮跳转至点餐界面
            case R.id.order_select:
                Intent selectIntent = new Intent(this, SelectActivity.class);
                //传递用户手机号phone
                selectIntent.putExtra("phone", phone);
                startActivity(selectIntent);
                break;
            //点击"设置"按钮跳转至设置界面
            case R.id.order_setting:
                Intent settingIntent = new Intent(this, SettingActivity.class);
                settingIntent.putExtra("phone", phone);
                startActivity(settingIntent);
                break;
        }
    }

    //更新UI
    public void updateUi() {
        //更新用户名
        user_name.setText(phone + "");
        //更新头像
        user_head.setImageResource(R.mipmap.user_icon);
        //获取订单信息
        getData();
        //更新listview
        Log.d(TAG, "updateUi: "+ orderListBeans);
        order_listview.setAdapter(new OrderItemAdapter(context, orderListBeans));
    }

    //获取到订单信息
    private void getData() {
        //通过手机号查询用户拥有的订单的信息
        List<UserOrder> userOrders = LitePal.where("phone = ?", phone).find(UserOrder.class);

        //获取商品的id和数量
        List<AllFood> allFoods = LitePal.findAll(AllFood.class);
        for (AllFood allFood : allFoods) {
            for (UserOrder userOrder : userOrders) {
                if (userOrder.getOrderId() == allFood.getId()) {
                    orderListBeans.add(new OrderListBean(allFood.getFoodName(),
                            allFood.getFoodContent(),
                            allFood.getFoodPrice() * userOrder.getNum(),
                            allFood.getImgId()
                    ));
                }
            }
        }
    }
}

